﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ufida.u9.ait.openapi.utils
{
    internal class FileUtils
    {
        /// <summary>
        /// 读取txt文件内容
        /// </summary>
        /// <param name="path">文件地址</param>
        public static string ReadTxtFile(string path)
        {
            StreamReader sr = new StreamReader(path);
            string content;
            StringBuilder sb = new StringBuilder();
            while ((content = sr.ReadLine()) != null)
            {
                //Console.WriteLine(content.ToString());
                sb.AppendLine(content);
            }
            sr.Close();
            return sb.ToString();
        }
        public static void WriteFile(string filePath, Stream filestream)
        {
            FileStream fs = null;
            try
            {
                //获取文件目录,并创建文件目录,支持多级
                string dir = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                //创建文件,并写入文件
                fs = new FileStream(filePath, FileMode.Create);
                int bufferSize = 2048;
                byte[] data = new byte[bufferSize];
                int length = 0;
                while ((length = filestream.Read(data, 0, bufferSize)) > 0)
                {
                    fs.Write(data, 0, length);
                }
                //fs.Close();
            }catch (Exception ex)
            {
                throw new Exception(ex.Message,ex);
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
           
        }
        public static void WriteBinFile(string path,string filename,byte[] bytes)
        {
            //获取U9路径
            string root = U9Utils.GetU9Dir();
            //拼接绝对路径
            string path1 = System.IO.Path.Combine(root, path);
            if (!Directory.Exists(path1))
            {
                //创建目录
                Directory.CreateDirectory(path1);
            }
            string filepath = path1 + "/" + filename;
            //if (File.Exists(filepath))
            //    File.Delete(filepath);
            FileStream fs = new FileStream(filepath, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes, 0, bytes.Length);
            bw.Close();
            fs.Close();
        }

        /// <summary>
        /// 读取文件创建时间
        /// </summary>
        /// <param name="path">文件地址</param>
        public static DateTime GetFileTime(string path)
        {
            System.IO.FileInfo fileInfo = null;
            try
            {
                fileInfo = new System.IO.FileInfo(path);
            }
            catch (Exception ex)
            {
                throw new Exception($"读取文件失败,path:[{path}]", ex);
                // 其他处理异常的代码
            }

            // 如果文件存在
            if (fileInfo != null && fileInfo.Exists)
            {
                return fileInfo.CreationTime;
            }
            else
            {
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// 打印指定文件的详细信息
        /// </summary>
        /// <param name="path">指定文件的路径</param>
        public static void PrintFileVersionInfo(string path)
        {
            System.IO.FileInfo fileInfo = null;
            try
            {
                fileInfo = new System.IO.FileInfo(path);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                // 其他处理异常的代码
            }
            // 如果文件存在
            if (fileInfo != null && fileInfo.Exists)
            {
                System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(path);
                Console.WriteLine("文件名称=" + info.FileName);
                Console.WriteLine("产品名称=" + info.ProductName);
                Console.WriteLine("公司名称=" + info.CompanyName);
                Console.WriteLine("文件版本=" + info.FileVersion);
                Console.WriteLine("产品版本=" + info.ProductVersion);
                // 通常版本号显示为「主版本号.次版本号.生成号.专用部件号」
                Console.WriteLine("系统显示文件版本：" + info.ProductMajorPart + '.' + info.ProductMinorPart + '.' + info.ProductBuildPart + '.' + info.ProductPrivatePart);
                Console.WriteLine("文件说明=" + info.FileDescription);
                Console.WriteLine("文件语言=" + info.Language);
                Console.WriteLine("原始文件名称=" + info.OriginalFilename);
                Console.WriteLine("文件版权=" + info.LegalCopyright);

                Console.WriteLine("文件大小=" + System.Math.Ceiling(fileInfo.Length / 1024.0) + " KB");
            }
            else
            {
                Console.WriteLine("指定的文件路径不正确!");
            }
            // 末尾空一行
            Console.WriteLine();
        }
    }
}
